/*
 * Copyright (C) 2018-2025 Toshiaki Maki <makingx@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package am.ik.yavi.arguments;

import java.util.Locale;
import java.util.function.Supplier;

import am.ik.yavi.core.ConstraintContext;
import am.ik.yavi.core.Validated;
import am.ik.yavi.core.ValueValidator;
import am.ik.yavi.fn.Function2;
import am.ik.yavi.fn.Validations;
import am.ik.yavi.jsr305.Nullable;

/**
 * Generated by https://github.com/making/yavi/blob/develop/scripts/generate-args.sh
 *
 * @since 0.7.0
 */
public class Arguments2Splitting<A1, A2, R1, R2> {

	protected final ValueValidator<? super A1, ? extends R1> v1;

	protected final ValueValidator<? super A2, ? extends R2> v2;

	public Arguments2Splitting(ValueValidator<? super A1, ? extends R1> v1,
			ValueValidator<? super A2, ? extends R2> v2) {
		this.v1 = v1;
		this.v2 = v2;
	}

	public <X> Arguments2Validator<A1, A2, X> apply(Function2<? super R1, ? super R2, ? extends X> f) {
		return new Arguments2Validator<A1, A2, X>() {

			@Override
			public Arguments2Validator<A1, A2, Supplier<X>> lazy() {
				return ((a1, a2, locale, constraintContext) -> Validations.apply((r1, r2) -> () -> f.apply(r1, r2),
						v1.validate(a1, locale, constraintContext), v2.validate(a2, locale, constraintContext)));
			}

			@Override
			public Validated<X> validate(@Nullable A1 a1, @Nullable A2 a2, Locale locale,
					ConstraintContext constraintContext) {
				return Validations.apply(f::apply, v1.validate(a1, locale, constraintContext),
						v2.validate(a2, locale, constraintContext));
			}
		};
	}

	public <A3, R3> Arguments3Splitting<A1, A2, A3, R1, R2, R3> split(ValueValidator<? super A3, ? extends R3> v3) {
		return new Arguments3Splitting<>(v1, v2, v3);
	}

}
